package com.teamwork.module03.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.teamwork.common.entity.module03.RtoR;
import com.teamwork.common.result.Result;
import com.teamwork.module03.mapper.UserMapper;
import com.teamwork.module03.mapper.UserIconMapper;
import com.teamwork.common.entity.module03.Reply;
import com.teamwork.common.entity.module03.dto.ReplyDto;
import com.teamwork.module03.mapper.ReplyMapper;
import com.teamwork.module03.mapper.RtoRMapper;
import com.teamwork.module03.service.ReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ReplyServiceImpl extends ServiceImpl<ReplyMapper, Reply> implements ReplyService {

    @Autowired
    private RtoRMapper rtoRMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserIconMapper userIconMapper;


    @Override
    public List<ReplyDto> listByComId(Long comId) {

        LambdaQueryWrapper<Reply> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Reply::getCommentId,comId).orderByDesc(Reply::getCreatetime);


        List<Reply> list = this.list(wrapper);
        if(list==null){
            //回复为空
            return Collections.emptyList();
        }
         List<ReplyDto> replyDtoList=new ArrayList<>();
        for (Reply reply : list) {
            Long userId = reply.getUserId();
            String nickname = userMapper.getById(userId).getNickname();
            String userIcon = userIconMapper.getUserIcon(userId);
            ReplyDto replyDto = new ReplyDto(reply, nickname, userIcon);
            replyDtoList.add(replyDto);
        }


        return replyDtoList;
    }

    /**
     * 根据文章id删除回复信息
     * @param articleId 文章id
     */
    @Override
    public boolean deleteByArticeId(Long articleId) {
        LambdaQueryWrapper<Reply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Reply::getArticleId,articleId);

        boolean b = this.remove(wrapper);
        if(b){
            log.info("根据文章id: {} 删除一天评论",articleId);
        }else{
            log.error("根据文章id:{}删除评论出错",articleId);
        }
         return b;
    }

    /**
     * 根据用户id删除回复
     * @param id 用户id
     */
    @Override
    public boolean deleteByUserId(Integer id) {
        LambdaQueryWrapper<Reply> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Reply::getUserId,id);
        boolean b = this.remove(wrapper);
        if(b) log.info("删除用户id:{} 发表的所有回复...",id);
        //else log.error("删除用户id:{} 发表的所有回复出错！！！",id);
        return b;

    }

    /**
     * 根据评论id删除这个评论地下的所有回复
     */
    @Override
    public boolean delteByCid(Integer cid) {
        LambdaQueryWrapper<Reply> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Reply::getCommentId,cid);

         this.remove(wrapper);

          return true;

    }

    /**
     * 回复某人的评论
     * @param userid  用户id
     * @param articleid  文章id
     * @param commentid  评论id，也可以是回复的id，递归
     * @param reply  回复内容
     * @return
     */
    @Override
    @Transactional
    public Result toreply(Integer userid, Integer articleid, Integer commentid, String reply) {

        if(userid==null||articleid==null||commentid==null||reply==null||reply.equals("")){
            return Result.failure("内容不能为空");
        }

        Reply replyone = new Reply();

        replyone.setUserId(Long.valueOf(userid));
        replyone.setArticleId(Long.valueOf(articleid));
        replyone.setCommentId(Long.valueOf(commentid));
        replyone.setComment(reply);
        replyone.setLikes(0);

        boolean b = this.save(replyone);
        if(!b){
            log.error("回复评论出错:用户id:{}",userid);
            return Result.failure("回复失败！后台错误！");
        }
        //拿到id
        Long re_from_id = replyone.getId();

        RtoR rtoR = new RtoR();
        rtoR.setReplyIdFrom(re_from_id);
        //因为是回复的评论，所以没有向回复的id
        rtoR.setReplyIdTo(null);
        rtoR.setCommentId(commentid);

        rtoRMapper.addOne(re_from_id,null,commentid,
                new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date()));

        return Result.success("回复成功！");


    }
}
